﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraBars;
using System.Linq;
using UniMana.Data;
using UniMana.Utils.Helper;
using UniMana.Utils.Data;

namespace UniMana.Win.Systems
{
    public partial class FrmGroupMananger : DevExpress.XtraEditors.XtraForm
    {
        private string m_Rule = UniMana.Utils.Configs.RuleConfig.GROUP_MANAGER;
        public FrmGroupMananger()
        {
            if(!SysUtils.CheckAccessByRule(m_Rule))
            {
                XtraMessageBox.Show(UniMana.Utils.Configs.RuleConfig.MESSAGEBOX);
                return;
            }

            InitializeComponent();
            LoadData();
            InitControlEvent();

            SysUtils.SetRuleToXucBar(xucBar1, m_Rule);
        }

        #region Khởi tạo XucBar
        public void InitControlEvent()
        {
            //xucBar1.AddNewVisible = false;
            //xucBar1.EditVisible = false;
            //xucBar1.DeleteVisible = false;
            //xucBar1.ExportExcelVisible = false;
            //xucBar1.ImportExcelVisible = false;

            //BarButtonItem btnLoad = new BarButtonItem();
            //btnLoad.Caption= "Tải dữ liệu";
            //xucBar1.Controls.Add(btnLoad);
            xucBar1.btnSave_Click += new Utils.Controls.xucBar.ButtonClickHandler(btnSave_Click);


            xucBar1.btnAddNew_Click += new Utils.Controls.xucBar.ButtonClickHandler(btnNew_Click);
            xucBar1.btnEdit_Click += new Utils.Controls.xucBar.ButtonClickHandler(btnEdit_Click);
            xucBar1.btnDelete_Click += new Utils.Controls.xucBar.ButtonClickHandler(btnDelete_Click);
        }
        #endregion

        #region [Sự kiện button]
        private void btnSave_Click(object sender, ItemClickEventArgs e)
        {

        }

        private void btnNew_Click(object sender, ItemClickEventArgs e)
        {
            if (!SysUtils.CheckAddByRule(m_Rule))
            {
                XtraMessageBox.Show(UniMana.Utils.Configs.RuleConfig.MESSAGEBOX);
                xucBar1.dlg.Close();
                return;
            }

            FrmCreateGroup f = new FrmCreateGroup();
            f.passData = new FrmCreateGroup.PassData(DelegateGroup);
            f.StartPosition = FormStartPosition.CenterParent;
            xucBar1.dlg.Close();
            f.ShowDialog();
        }

        private void btnEdit_Click(object sender, ItemClickEventArgs e)
        {
            if (!SysUtils.CheckEditByRule(m_Rule))
            {
                XtraMessageBox.Show(UniMana.Utils.Configs.RuleConfig.MESSAGEBOX);
                xucBar1.dlg.Close();
                return;
            }

            xucBar1.dlg.Close();
            if (grvSystem_Group.GetFocusedRow() != null)
            {
                
                System_GroupInfo sgi = new System_GroupInfo(int.Parse(grvSystem_Group.GetFocusedRowCellValue("System_GroupKey").ToString()));
                FrmCreateGroup f = new FrmCreateGroup(sgi);
                f.Text = "Sửa thông tin nhóm:" + sgi.Properties.Name ;
                f.passData = new FrmCreateGroup.PassData(DelegateGroup);
                f.StartPosition = FormStartPosition.CenterParent;
                f.ShowDialog();
            }
        }

        private void btnDelete_Click(object sender, ItemClickEventArgs e)
        {
            if (!SysUtils.CheckDeleteByRule(m_Rule))
            {
                XtraMessageBox.Show(UniMana.Utils.Configs.RuleConfig.MESSAGEBOX);
                xucBar1.dlg.Close();
                return;
            }

            xucBar1.dlg.Close();
            DialogResult dagResult = MessageBox.Show("Bạn thật sự muốn xóa nhóm khỏi hệ thống", "Thông báo", MessageBoxButtons.OKCancel);
            if (dagResult == DialogResult.Cancel)
            {
                return;
            }
            if (grvSystem_Group.GetFocusedRow() != null)
            {
                System_GroupInfo sgi = new System_GroupInfo(int.Parse(grvSystem_Group.GetFocusedRowCellValue("System_GroupKey").ToString()));
                DataTable dtListUser = System_UserData.GetUserByGroup(sgi.Properties.System_GroupKey).ToDataTable();
                if (dtListUser.Rows.Count >0)
                {
                    MessageBox.Show("Nhóm có nhiều người dùng. Hãy xóa người dùng khỏi hệ thống trước");
                }
                else
                {
                    DataTable dt = System_Group_RuleData.GetByGroup(sgi.Properties.System_GroupKey).ToDataTable();
                    if (dt != null)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            System_Group_RuleInfo sgri = new System_Group_RuleInfo(Convert.ToInt16(dr["System_GroupKey"]), Guid.Parse(dr["System_RuleKey"].ToString()));
                            sgri.Delete();
                            sgri.Save();
                        }
                    }

                    sgi.Delete();
                    sgi.Save();
                    grvSystem_Group.DeleteSelectedRows();
                }
            }
        }
        #endregion

        public void LoadData()
        {
           grcSystem_Group.DataSource = System_GroupData.GetList(new DBMain()).ToDataTable();
        }

        public void DelegateGroup(System_GroupInfo sgi)
        {
            DataTable dt = this.grcSystem_Group.DataSource as DataTable;
            DataRow dRow  = (from DataRow dr in dt.Rows where dr["System_GroupKey"].ToString() == sgi.Properties.System_GroupKey.ToString() select dr).FirstOrDefault();
            
            if (dRow != null)
            {

                dRow["System_GroupKey"] = sgi.Properties.System_GroupKey;
                dRow["Name"] = sgi.Properties.Name;
                dRow["Description"] = sgi.Properties.Description;
            }
            else
            {
                DataRow dr = dt.NewRow();
                dr["System_GroupKey"] = sgi.Properties.System_GroupKey;
                dr["Name"] = sgi.Properties.Name;
                dr["Description"] = sgi.Properties.Description;
                dt.Rows.Add(dr);
            }
        }

        private void grvSystem_Group_DoubleClick(object sender, EventArgs e)
        {
            if (grvSystem_Group.GetFocusedRow() != null)
            {
                System_GroupInfo sgi = new System_GroupInfo(int.Parse(grvSystem_Group.GetFocusedRowCellValue("System_GroupKey").ToString()));
                FrmCreateGroup f = new FrmCreateGroup(sgi);
                f.Text = "Thông tin nhóm: "+sgi.Properties.Name;
                f.pclChangeMember.Enabled = false;
                f.pclSaveAndClose.Enabled= false;
                f.btnAdd.Enabled= false;
                f.btnRemove.Enabled= false;

                f.passData = new FrmCreateGroup.PassData(DelegateGroup);
                f.StartPosition = FormStartPosition.CenterParent;
                f.ShowDialog();
            }
        }

    }
}